package com.practice.book.controller;

import com.practice.book.constant.Constants;
import com.practice.book.enums.BookStatusEnum;
import com.practice.book.model.BookInfo;
import com.practice.book.model.PageRequest;
import com.practice.book.model.PageResponse;
import com.practice.book.model.Result;
import com.practice.book.service.BookService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2025-01-11
 * Time: 14:01
 */
@RestController
@Slf4j
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        log.info("添加图书,bookInfo:{}",bookInfo);
        //1.参数校验
        if(!StringUtils.hasLength(bookInfo.getBookName())
        ||!StringUtils.hasLength(bookInfo.getAuthor())
        ||bookInfo.getCount() == null
        ||bookInfo.getPrice() ==null
        ||!StringUtils.hasLength(bookInfo.getPublish())) {
            return "参数不合法，重新传入";
        }

        try{
            //2.插入信息
            Integer result = bookService.insertBook(bookInfo);
            if(result == 1) {
                return "";
            }
        }
        catch (Exception e) {
            log.error("数据插入发生异常,e:{}",e);
        }
        return "插入失败，请重新插入";
    }

    @RequestMapping("/getListByPage")
    public Result<PageResponse<BookInfo>> getListByPage(PageRequest pageRequest, HttpServletRequest request) {
        log.info("获取图书列表，pageRequest: {}",pageRequest);
        /*HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constants.SESSION_USER_KEY) == null ||
                "".equals(session.getAttribute(Constants.SESSION_USER_KEY))) {
            return Result.unLogin();
        }*/
        PageResponse<BookInfo> bookInfoPageResponse = new PageResponse<>();
        try {
            bookInfoPageResponse = bookService.getListByPage(pageRequest);
        }catch (Exception e) {
            log.error("获取图书列表失败!");
            return Result.loginButFail();
        }
        return Result.success(bookInfoPageResponse);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        log.info("获取图书信息,bookId: "+ bookId);
        return bookService.queryBookById(bookId);
    }

    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        log.info("更新图书,bookInfo:{}",bookInfo);
        try {
            Integer result = bookService.updateBook(bookInfo);
            if(result > 0) {
                return "";
            }
            return "图书更新失败了!";
        }catch (Exception e){
            log.error("更新图书失败,e:",e);
            return "数据更新失败!";
        }
    }

    @RequestMapping(value = "/deleteBook",produces = "application/json")
    public String deleteBook(Integer bookId) {
        log.info("删除图书,bookId:{}",bookId);
        try {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setStatus(BookStatusEnum.DELETE.getCode());
            bookInfo.setId(bookId);
            Integer result = bookService.updateBook(bookInfo);
            System.out.println(result);
            if(result > 0) {
                return "";
            }
            return "图书删除失败了!";
        }catch (Exception e){
            log.error("删除图书失败,e:",e);
            return "数据删除失败!";
        }
    }

    @RequestMapping("/batchDeleteBook")
    public Boolean batchDeleteBook(@RequestParam("ids") List<Integer> ids) {
        log.info("批量删除图书，ids:{}",ids);
        try {
            bookService.batchDelete(ids);
            List<Integer> list = ids;
            System.out.println(list);
            return true;
        }catch (Exception e) {
            log.error("批量删除图书失败，e:",e);
        }
        return false;
    }
}
